﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThaiHoa.Win.QLSX.Entity;
using ThaiHoa.Win.QLSX.DAL;
using System.Data.SqlClient;
using JATProject.DataAccess;
using ThaiHoa.Win.QLSX.Kho;

namespace ThaiHoa.Win.QLSX.QLKH.Business
{
    public partial class TaoMoiHopDong : Form
    {
        Access arc;
        static string conStr = ConnectDB.GetStringConnection();
        static SqlConnection con = new SqlConnection(conStr);
        SqlCommand cmd = con.CreateCommand();
        private KhoDBContext db;
        public String MaKhach;
        public String MaSanPham;
        public TaoMoiHopDong()
        {
            InitializeComponent();
        }
        

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            
        }

        private void TaoMoiHopDong_Load(object sender, EventArgs e)
        {
            db = new KhoDBContext();           
            List<DoiTac> listDoiTac = db.DoiTac.Where(d => d.TrangThai).ToList();
            cbxCustomer.DataSource = listDoiTac;


            List<ThanhPham> listThanhPham = db.ThanhPham.Where(d => d.TrangThai).ToList();
            comboBox2.DataSource = listThanhPham;

            arc = new Access(this);

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            ThanhPham thanhpham = comboBox2.SelectedItem as ThanhPham;
            if (thanhpham == null)
                return;
            MaSanPham = thanhpham.MaMatHang.ToString();
            
        }


        private void pictureBox1_Click(object sender, EventArgs e)
        {

            arc.ShowFormDoiTacDetail(Guid.Empty);

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (cbxCustomer.SelectedText.ToString() == null)
            {
                lblchkTen.Visible = true;
                return;
            }
            else
            {
                lblchkTen.Visible = false;
            }
            if (textBox1.Text == "")
            {
                lblchkMa.Visible = true;
                return;
            }
            else
            {
                lblchkMa.Visible = false;
            }
            if (comboBox2.SelectedText.ToString() == null)
            {
                lblchkSanpham.Visible = true;
                return;
            }
            else
            {
                lblchkSanpham.Visible = false;
            }
            if (textBox2.Text == "")
            {
                lblchkTongluong.Visible = true;
                return;
            }
            else
            {
                lblchkTongluong.Visible = false;
            }
            int number;
            if (Int32.TryParse(textBox2.Text, out number) == false)
            {
                label10.Visible = true;
                return;
            }
            else
            {
                label10.Visible = false;
            }


            int result = DateTime.Compare(dateTimePicker1.Value.Date, dateTimePicker2.Value.Date);
            if (result > 0)
            {
                lblchkStart.Visible = true;
                label3.Visible = false;
                return;
            }
            else
            {
                lblchkStart.Visible = false;
            }
            if (result == 0)
            {
                label3.Visible = true;
                lblchkStart.Visible = true;
                return;
            }
            else
            {
                label3.Visible = false;
            }

            try
            {
                con.Open();


                cmd.CommandText = "insert into [Order] (CodeOrder,StartDate,DeapLine,ProductAmount,CodeProduct,NameProduct,NameCustomer) values (@CodeOrder,@StartDate,@DeapLine,@ProductAmount,@CodeProduct,@NameProduct,@NameCustomer)";
                cmd.CommandType = CommandType.Text;

                cmd.Parameters.AddWithValue("@CodeOrder", textBox1.Text.Trim());
                cmd.Parameters.AddWithValue("@StartDate", dateTimePicker1.Value.ToString("MM/dd/yyyy"));
                cmd.Parameters.AddWithValue("@DeapLine", dateTimePicker2.Value.ToString("MM/dd/yyyy"));
                cmd.Parameters.AddWithValue("@ProductAmount", textBox2.Text.Trim());
                cmd.Parameters.AddWithValue("@CodeProduct", MaSanPham.ToString().Trim());
                cmd.Parameters.AddWithValue("@NameProduct", cbxCustomer.SelectedItem.ToString());                
                cmd.Parameters.AddWithValue("@NameCustomer", comboBox2.SelectedItem.ToString());

                cmd.ExecuteNonQuery();
                MessageBox.Show("Success!");
                this.Close();
            }
            catch
            {
                MessageBox.Show("Error!");
            }
            finally
            {
                con.Close();
            }
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {

        }

     
    }
}
